<?php
namespace common\core\base;

use Yii;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;

/**
 * Class BaseActiveRecord
 *
 * @author Xuegui Yuan <yuanxuegui@163.com>
 */
class BaseActiveRecord extends \yii\db\ActiveRecord
{

    public static function findOne($condition = null, $orderBy = null, $with = null, $params = [])
    {
        $query = self::findQuery($condition, $orderBy, $with, $params);
        return $query->one();
    }

    public static function findAll($condition = null, $orderBy = null, $limit = null, $with = null, $params = [])
    {
        $query = self::findQuery($condition, $orderBy, $with, $params);
        if ($limit != null) {
            $query->limit($limit);
        }
        return $query->all();
    }

    public static function findQuery($condition = null, $orderBy = null, $with = null, $params = [])
    {
        $query = static::find();
        
        if ($with !== null) {
            if (is_string($with)) {
                $query->innerJoinWith($with);
            } else {
                if (isset($with['type'])) {
                    $type = $with['type'];
                } else 
                    if (isset($with[1])) {
                        $type = $with[1];
                    } else {
                        $type = 'INNER JOIN';
                    }
                
                $query->joinWith($with[0], true, $type);
            }
        }
        
        if ($condition !== null && ! empty($condition)) {
            if (! ArrayHelper::isAssociative($condition)) {
                // query by primary key
                $primaryKey = static::primaryKey();
                if (isset($primaryKey[0])) {
                    $condition = [
                        $primaryKey[0] => $condition
                    ];
                } else {
                    throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');
                }
            }
            $query->andWhere($condition);
        }
        
        if ($orderBy != null) {
            $query->orderBy($orderBy);
        }
        return $query;
    }

    public static function leftJoinWith($with)
    {
        $query = self::find();
        $query->joinWith($with, true, 'LEFT JOIN');
        return $query;
    }

    public static function rightJoinWith($with)
    {
        $query = self::find();
        $query->joinWith($with, true, 'RIGHT JOIN');
        return $query;
    }

    public function afterValidate()
    {
        parent::afterValidate();
        if ($this->hasErrors()) {
            setErrorMessage($this->getFirstErrors());
            info($this->errors, self::className());
        }
    }

    public function attributeLabels()
    {
        return static::getAttributeLabels();
    }

    public static function getAttributeLabels($attribute = null)
    {
        return [];
    }

    public function afterDelete()
    {
        parent::afterDelete();
        $this->clearCache();
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        $this->clearCache();
    }

    public function clearCache()
    {}
}
